<?php

namespace App\Models\PersonInfoManagement;

use App\Models\CommonModel;
use App\Models\Constant\Errors;
use App\Models\Constant\Fields;
use App\Models\Error;
use App\Models\SystemManagement\Department;

class PersonInfo extends CommonModel
{
    protected $table = 'person_infos';
    public $primaryKey = 'id';

    protected $hidden = ['created_at','updated_at','deleted_at'];

    /**
     * @return mixed
     * 获取部门数据
     * 转换成数组返回
     */
    public static function getDataToArr(){
        $query = self::getTableDataDataToArr(new Department());
        return $query;
    }

    /**
     * @return Error
     * 查询列表 带分页
     */
    static function getData($per){
        $query = parent::getTableData(new self(),$per);
        return $query;
    }

    /**
     * @return mixed
     * 查询列表
     */
    public function getAll(){
        return parent::getAllList(new self());
    }

    /**
     * @param $id
     * @return Error
     * 查看当前ID详情
     */
    static function getOneData($id){
        $query = self::find($id);
        return $query;
    }

    /**
     * @param $data
     * @return Error
     * 添加人员管理信息
     */
    static function addData($data){
        $param = new self();
        $re = $param->create($data);
        return $re;
    }

    /**
     * @param $data
     * @param $id
     * @return Error
     * 修改人员管理信息
     */
    static function updateData($data,$id){
        $re = self::updateTableData($data,$id,new self());
        return $re;
    }

    /**
     * @param $ids
     * @return Error
     * 批量删除人员管理信息
     */
    static function del($ids){
        $err = new Error();
        $query = self::whereIn(Fields::ID,$ids)->delete();
        if($query){
            $err->setError(Errors::OK);
            $err->setMessage('删除成功');
        }else{
            $err->setError(Errors::FAILED);
            $err->setMessage('删除失败');
        }
        return $err;
    }

    /**
     * @param $name
     * @return mixed
     * 通过姓名查找
     */
    public static function getByName($name){
        $query = self::where(Fields::Name,$name)->get();
        return $query;
    }

    /**
     * @param $name
     * @return mixed
     * 通过部门查找
     */
    public static function getByDepartment($id){
        $query = Department::where(Fields::ID,$id)
            ->select('id','name')
            ->with('personInfo')
            ->get();
        return $query;
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     * 班次信息
     */
    public function banci(){
        return $this->belongsTo(self::$SYSTEM.'\Banci',Fields::BC_ID,Fields::ID)
            ->select('id','name');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     * 作业区域
     */
    public function workspace(){
        return $this->belongsTo(self::$SYSTEM.'\WorkSpace',Fields::WS_ID,Fields::ID)
            ->select('id','name');
    }
}
